<?php
declare(strict_types=1);

namespace app\api\controller;

use support\Request;
use app\basic\ApiController;
use app\api\logic\PayLogic;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("支付接口")
 */
class PayController extends ApiController
{
    protected $noNeedLogin = ['wechatNotify'];
    protected $noNeedSign = ['wechatNotify'];

    protected $payLogic;

    public function __construct()
    {
        $this->logic = new PayLogic();
        parent::__construct();
    }

    /**
     * @Apidoc\Title("订单支付")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("order_id", type="int", require=true, desc="订单ID")
     * @Apidoc\Param("pay_type", type="int", require=true, desc="支付方式:1=余额支付 2=微信支付 3=支付宝支付")
     */
    public function pay(Request $request)
    {
        $params = $request->post();
        $params['client'] = $request->header('client');
        $result = $this->logic->pay($this->auth->getUserId(), $params);
        if (!$result) {
            return $this->fail($this->logic->getError());
        }
        return $this->success($result);
    }

    /**
     * @Apidoc\Title("测试回调")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("order_id", type="int", require=true, desc="订单ID")
     */
    public function test(Request $request)
    {
        $params = $request->post();
        $this->logic->handlePaySuccess($params);
    }

    /**
     * @Apidoc\Title("微信回调域名")
     * @Apidoc\Method("POST")
     */
    public function wechatNotify(Request $request)
    {
        $params = $request->all();
        write_log($params, runtime_path().'/wechat_notify');
        return $this->logic->payNotify($params,"2");
    }
}